package com.wmh.baseservice.common.utils.returnbody;

import com.alibaba.fastjson.JSONObject;
import com.wmh.baseservice.common.utils.LogUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 返回数据实体
 *
 * @author
 */
@Component
public class ReturnBody {


    @Value("${logPrint}")
    private Boolean print;

    private static Boolean staticPrint;

    @PostConstruct
    private void getPrint() {
        staticPrint = print;
    }

    public static String message(Message message, ResultCodeInfo codeInfo) {
        ResultInfo resultInfo = new ResultInfo(message.getValue(), codeInfo.getCode(), codeInfo.getResult());
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(resultInfo));
        return jsonObject.toString();
    }

    public static String message(Message message, Code code, Object result) {
        ResultInfo resultInfo = new ResultInfo(message.getValue(), code.getValue(), result);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(resultInfo));
        if (staticPrint) {
            LogUtil.info("输出" + jsonObject.toString());
        }
        return jsonObject.toString();
    }
    

    public static String message(Message message, Code code, Object result, boolean hasPrint) {
        ResultInfo resultInfo = new ResultInfo(message.getValue(), code.getValue(), result);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(resultInfo));
        if (hasPrint) {
            LogUtil.info("输出" + jsonObject.toString());
        }
        return jsonObject.toString();
    }


    public static String message(Message message, Integer code, Object result) {
        ResultInfo resultInfo = new ResultInfo(message.getValue(), code, result);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(resultInfo));
        return jsonObject.toString();
    }


    public static String success() {
        return message(Message.SUCCESS, ResultCodeInfo.SUCCESS);
    }

    public static String success(Object result) {
        return message(Message.SUCCESS, Code.SUCCESS, result);
    }

    public static String success(Code code, Object result) {
        return message(Message.SUCCESS, code, result);
    }

    public static String success(Object result, boolean hasPrint) {
        return message(Message.SUCCESS, Code.SUCCESS, result, hasPrint);
    }

    public static String error() {
        return message(Message.ERROR, ResultCodeInfo.ERROR);
    }

    public static String error(ResultCodeInfo resultCodeInfo) {
        return message(Message.ERROR, resultCodeInfo);
    }

    public static String error(Object result) {
        return message(Message.ERROR, Code.ERROR, result);
    }
    public static String error(Object result,boolean hasPrint) {
        return message(Message.ERROR, Code.ERROR, result,hasPrint);
    }

    public static String error(Code code, Object result) {
        return message(Message.ERROR, code, result);
    }

    public static String error(Integer code, Object result) {
        return message(Message.ERROR, code, result);
    }


}
